import { cloud } from '@hw-agconnect/cloud-server';
import { user_push_token } from './user_push_token'; // location是Server Model
// default是location对象类型使用的存储区名
// 创建链接数据库的句柄
const collection = cloud.database({ zoneName: 'default' }).collection(user_push_token);


export class SetUserPushToken {
  upset(userPushTokens: user_push_token[]) {

    console.log('JSON:' + JSON.stringify(userPushTokens))
    const mapsArr = userPushTokens.map(ut => user_push_token.parseFrom(ut))
    console.log('JSON2:' + JSON.stringify(mapsArr))

    return new Promise<{ result: number }>((resolve, reject) => {
      collection.upsert(mapsArr)
        .then(result => resolve({ result }))
        .catch(err => reject(err))
    });
  }
}
